package utils.independent;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import swissmap.Constants;

public class HiperMapbuilder {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] res={"200k","100k","50k","25k","10k"}; //,"20k","10k"};
		String head="CompeGPS MAP File\r\n" +
				"<Header>\r\n" +
				"Version=1\r\n" +
				"Projection=1,Lat/Long,\r\n" +
				"Coordinates=1\r\n" +
				"Datum=WGS 84\r\n" +
				"</Header>\r\n" +
				"<HiperMapLayers>\r\n";
		
		String foot="</HiperMapLayers>";
//<HLayer File="100k\100k_map_00_00.RMAP" visible="1" MaxZoomLevel="35" MaxZoomLevel="9999"/>
//<HLayer File="50k\50k_map_00_00.RMAP" visible="1" MaxZoomLevel="35" MinZoomLevel="20"/>
//<HLayer File="50k\50k_map_00_01.RMAP" visible="1" MaxZoomLevel="35"/>
//<HLayer File="50k\50k_map_01_00.RMAP" visible="1" MaxZoomLevel="35"/>
//<HLayer File="50k\50k_map_01_01.RMAP" visible="1" MaxZoomLevel="35"/>
		
		Map minZoom=new HashMap<String, Integer>();
		Map maxZoom=new HashMap<String, Integer>();
		
		int tmp;
		
		maxZoom.put("200k",9999);
		tmp=120;
		minZoom.put("200k",tmp);
		maxZoom.put("100k",tmp);
		tmp=30;
		minZoom.put("100k",tmp);
		maxZoom.put("50k",tmp);
		tmp=15;
		minZoom.put("50k",tmp);
		maxZoom.put("25k",tmp);
		
		tmp=7;
		minZoom.put("25k",tmp);
		maxZoom.put("10k",tmp);
		minZoom.put("10k",0);
		

		String maps="";
		for(String curres:res){
			for(int r=0;r<99;r++){
				for(int c=0;c<99;c++){
					File rtmap=new File(Constants.basePath+curres+File.separator+curres+"-"+((c<10)?"0"+c:c)+"-"+((r<10)?"0"+r:r)+".RMAP");
					System.out.println(rtmap.getAbsolutePath());
					if(rtmap.exists()){
						System.out.println(rtmap.getAbsolutePath());
						maps+="<HLayer File=\""+curres+"\\"+curres+"_map_"+((c<10)?"0"+c:c)+"_"+((r<10)?"0"+r:r)+".RMAP\"" +
						" visible=\"1\" MinZoomLevel=\""+minZoom.get(curres)+"\" MaxZoomLevel=\""+maxZoom.get(curres)+"\"/>\r\n";
					}
				}
			}
		}
		
		File imp=new File(Constants.basePath+"swiss_full.imp");
		try {
			FileWriter fstream = new FileWriter(imp);
	        BufferedWriter out = new BufferedWriter(fstream);
	        out.write(head+maps+foot);
	        out.close();
	        System.out.println("IMP "+imp.getName()+" file written!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
